import React, { Component } from 'react';
import { throttle } from 'modules/RX';

export default class ClickView extends Component {
    static defaultProps = {
        onClick: () => {},
        tag: 'div',
        throttle: 0,
        stopPropagation: false, // 是否组织冒泡
        preventDefault: false,
    };

    handleClick = (event, ...args) => {
        const { stopPropagation, preventDefault } = this.props;
        stopPropagation && event.stopPropagation();
        preventDefault && event.preventDefault();
        this.realHandler(event, ...args);
    };

    realHandler = throttle(this.props.onClick, this.props.throttle);

    render() {
        const {
            tag: Tag, onClick, throttle: a, stopPropagation, preventDefault, ...otherProps
        } = this.props;

        return (
            <Tag {...otherProps} onClick={this.handleClick}>
                {this.props.children}
            </Tag>
        );
    }
}
